import type { PromptRequest } from '@stagewise/extension-toolbar-srpc-contract';
import * as vscode from 'vscode';
import { OpenWebAuthService } from '../services/openweb-auth-service';

export async function callCopilotAgent(request: PromptRequest): Promise<void> {
  const prompt =
    `${request.prompt}` +
    `${request.files ? `\n\n use the following files: ${request.files.join('\n')}` : ''}` +
    `${request.images ? `\n\n use the following images: ${request.images.join('\n')}` : ''}`;

  await vscode.commands.executeCommand('workbench.action.chat.openAgent');
  await vscode.commands.executeCommand('workbench.action.chat.sendToNewChat', {
    inputValue: prompt,
  });
}

export async function devAgent(request: PromptRequest): Promise<void> {
  const prompt =
    `${request.prompt}` +
    `${request.files ? `\n\n use the following files: ${request.files.join('\n')}` : ''}` +
    `${request.images ? `\n\n use the following images: ${request.images.join('\n')}` : ''}`;

  try {
    // 获取用户信息并打印
    const authService = OpenWebAuthService.getInstance();
    const userInfo = await authService.getUserInfo();
    console.log('[callDevAgent] 用户信息:', userInfo);

    // 复制到剪贴板
    await vscode.env.clipboard.writeText(prompt);

    // 显示成功消息
    vscode.window.showInformationMessage('✅ 请求内容已复制到剪贴板');
  } catch (error: any) {
    console.error('复制到剪贴板失败:', error);
    vscode.window.showErrorMessage(
      `复制到剪贴板失败: ${error.message || '未知错误'}`,
    );
  }

  // await vscode.commands.executeCommand('workbench.action.chat.openAgent');
  // await vscode.commands.executeCommand('workbench.action.chat.sendToNewChat', {
  //   inputValue: prompt,
  // });
}
